/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.mwas.proteana.math.factorials;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

/**
 *
 * @author loskov
 */
public class FactorialPochammer extends Factorial {
    
    public FactorialPochammer(int n) {
        super(n);
    }

    public FactorialPochammer(BigDecimal n) {
        super(n);
    }

    @Override
    protected BigDecimal calculate() {
        if (base.intValue() < 200) {
            throw new ArithmeticException("Base is too low for this algorithm.");
        } else {
            BigDecimal eDec = new BigDecimal(Math.E);
            BigDecimal nDivE = base.divide(eDec, new MathContext(16, RoundingMode.CEILING));
            BigDecimal powerPart = nDivE.pow(base.intValue());
            BigDecimal sqrtPart = new BigDecimal(Math.sqrt(2 * Math.PI * base.doubleValue()));
            result = powerPart.multiply(sqrtPart);
            result = result.setScale(0, RoundingMode.CEILING); // factorial has to be integer, so rounding
            return result;
        }

    }

    @Override
    public BigDecimal getResult() {
        return result;
    }

}
